﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Elements
{
    public class HttpHeaderCookie
    {
        public List<SingleCookieHeader> lstCookies = new List<SingleCookieHeader>();

        public HttpHeaderCookie()
        {
        }
    }

    public class SingleCookieHeader
    {
        public string name = string.Empty;
        public string value = string.Empty;

        public SingleCookieHeader(string name, string value)
        {
            this.name = name;
            this.value = value;
        }

        public static SingleCookieHeader[] Parse(string cookie)
        {
            SingleCookieHeader[] lstRtn = new SingleCookieHeader[cookie.Split(';').Length];
            for (int i = 0; i < lstRtn.Length; i++ )
            {
                string name = string.Empty;
                string value = string.Empty;

                try
                {

                    name = cookie.Split(';')[i].Split('=')[0].Trim();
                    value = cookie.Split(';')[i].Split('=')[1].Trim();
                }
                catch
                {
                    name = cookie.Split(';')[i];
                }
                lstRtn[i] = new SingleCookieHeader(name, value);

                /*
                string name = string.Empty;
                string value = string.Empty;

                
                try
                {
                    name = cookie.Split('=')[0].Trim();
                    value = string.Empty;

                    if (cookie.Contains(";"))
                        value = cookie.Split('=')[1].Split(';')[0].Trim();
                    else
                        value = cookie.Split('=')[1].Trim();
                }
                catch
                {
                    return null;
                }*/
            }

            return lstRtn;
            //return new SingleCookieHeader(name, value);
        }

        public override string ToString()
        {
            return string.Format("{0}={1};", name, value);
        }
    }
}
